﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.controllers.data {
    import net.wooga.pets.loading.vos.*;
    import net.wooga.pets.events.*;
    import net.wooga.pets.models.*;
    import net.wooga.pets.interfaces.*;
    import com.adobe.serialization.json.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.models.world.*;

    public class HandleElementCommand extends Command {

        public var event:HandleElementEvent;
        public var enviromentModel:EnvironmentModel;
        public var sessionModel:SessionModel;
        public var shopModel:ShopModel;
        public var worldModel:WorldModel;
        public var worldFactory:WorldFactory;

        override public function execute():void{
            switch (this.event.type){
                case HandleElementEvent.CREATE_ELEMENT:
                    this.sendCreate();
                    break;
                case HandleElementEvent.UPDATE_ELEMENT:
                    this.sendUpdate();
                    break;
                case HandleElementEvent.REMOVE_ELEMENT:
                    this.sendRemove();
                    break;
            };
        }
        private function sendCreate():void{
            this.sendTaskCall("create_element", GameConstants.BUY_ELEMENT, this.onCreate);
        }
        private function onCreate(_arg1:ResultVO):void{
            var _local2:Object;
            var _local3:int;
            if (_arg1.success){
                _local2 = _arg1.data.create_element;
                if (((((_local2.element.element_type) && (_local2.changes.coins_old))) && (_local2.changes.coins_new))){
                    _local3 = (_local2.changes.coins_old - _local2.changes.coins_new);
                    dispatch(new TrackingEvent(TrackingConst.TRACK, TrackingConst.BUY_DECO, {type:_local2.element.element_type, price_coins:_local3}));
                };
                dispatch(new InfoEvent(EventConstants.UPDATE_USER_DATA, null, _local2));
                this.addElement(_local2.element);
                dispatch(new InfoEvent(EventConstants.PLAY_EFFECT_SOUND, GameConstants.SOUND_SELL_SOMETHING));
            };
        }
        private function sendUpdate():void{
            var _local1:String;
            var _local2:InfoEvent;
            if (this.event.data.room_id == null){
                _local1 = JSON.encode(this.event.data);
                _local2 = new InfoEvent(EventConstants.REPORT_PROBLEM, "cannot move element as room_id is null", _local1);
                dispatch(_local2);
            } else {
                this.sendCall("update_element", this.onUpdate, this.event.data);
            };
        }
        private function onUpdate(_arg1:ResultVO):void{
            if (_arg1.success){
                dispatch(new ShopEvent(ShopEvent.UPDATE_SHOP_ITEMS));
                this.addElement(_arg1.data.update_element);
            };
        }
        private function sendRemove():void{
            this.sendTaskCall("remove_element", GameConstants.STORE_ELEMENT, this.onRemove);
        }
        private function onRemove(_arg1:ResultVO):void{
            if (_arg1.success){
                this.shopModel.increaseNumberOfItem(this.event.elementType, this.event.id);
                dispatch(new ShopEvent(ShopEvent.UPDATE_SHOP_ITEMS));
            };
        }
        private function sendTaskCall(_arg1:String, _arg2:String, _arg3:Function):void{
            var _local4:Array = [new RequestVO(_arg1, this.event.data)];
            var _local5:Object = {taskType:_arg2, itemType:this.event.elementType, onResult:_arg3};
            var _local6:TaskRequestEvent = new TaskRequestEvent(EventConstants.SEND_TASK_REQUEST, _local4, _local5);
            dispatch(_local6);
        }
        private function sendCall(_arg1:String, _arg2:Function, _arg3:Object=null):void{
            var _local4:RequestEvent = new RequestEvent(EventConstants.SEND_REQUEST, _arg2);
            _local4.addRequest(_arg1, _arg3);
            dispatch(_local4);
        }
        private function addElement(_arg1:Object):void{
            var _local4:ElementVO;
            var _local5:HandleElementEvent;
            var _local2:ElementVO = this.worldFactory.createElement(_arg1);
            var _local3:ICustomizable = (this.worldModel.getRoomById(_local2.roomId) as ICustomizable);
            if (_local2.isBackground){
                _local4 = _local3.removeBackgroundOf(_local2.destination);
                if (_local4){
                    dispatch(new InfoEvent(EventConstants.ON_ELEMENT_REMOVED, _local3.id, _local4));
                    _local5 = new HandleElementEvent(HandleElementEvent.REMOVE_ELEMENT, _local4.id, _local4.type);
                    dispatch(_local5);
                };
            };
            _local3.addElement(_local2);
            dispatch(new InfoEvent(EventConstants.ADD_ELEMENT, _local2.roomId, _local2));
        }

    }
}//package net.wooga.pets.controllers.data 
